# 公共
import sqlite3
# 窗口
import tkinter
# 数据库
from db.model import db_init
import db.db_manage as dbm
import db.model
from tkinter import messagebox
from tkinter import IntVar

# ---分割线---

account_flag = 0
rule = [""]
note_radios = []


# 数据库
db_init()

# 登录
def Account_register(tip):
    global user_id, account_flag, account
    account = entry_register1.get()
    password = entry_register2.get()
    res = dbm.user_add(account, password)
    if res == "该用户已存在":
        tip["text"] = "该用户已存在"
    else:
        user_id = res
        account_flag = 1
        root_register.destroy()


def Account_Sign_in(tip):  # 登录
    global user_id, account_flag, account
    account = entry_mine1.get()
    password = entry_mine2.get()
    res = db.db_manage.find_user(account, password)
    if res == 0:
        tip["text"] = "账户不存在"
        return 0
    if res != 0 or res != -1:
        tip["text"] = "登录成功"
        account_flag = 1
        root_mine0.destroy()
        user_id = res[0]
    elif res == 0:
        tip["text"] = "该用户不存在"
    elif res == -1:
        tip["text"] = "账户密码错误"

def register():  # 注册
    global root_register, entry_register1, entry_register2
    root_register = tkinter.Tk()
    root_register.title("register")
    root_register.geometry("500x200")

    root_mine0.destroy()
    root_tips = tkinter.Label(root_register, text="请输入用户名与密码")
    button_register = tkinter.Button(root_register, text="确定", command=lambda: Account_register(root_tips))
    entry_register1 = tkinter.Entry(root_register, width=30)
    entry_register2 = tkinter.Entry(root_register, width=30)
    entry_register1.delete(0, "end")
    entry_register1.insert(0, "请输入用户名")
    entry_register2.delete(0, "end")
    entry_register2.insert(0, "请输入密码")

    root_tips.pack()
    entry_register1.pack()
    entry_register2.pack()
    button_register.pack()
    root_register.protocol("WM_DELETE_WINDOW", mine_on_closing)
    root_register.mainloop()

def mine_on_closing():
    exit()

def mine_0():  # 登录/注册 功能主界面
    global root_mine_tips, entry_mine1, entry_mine2, root_mine0
    root_mine0 = tkinter.Tk()
    root_mine0.title("登录")
    root_mine0.geometry("500x200")

    root_mine_tips = tkinter.Label(root_mine0, text="您好，请先登录")
    entry_mine1 = tkinter.Entry(root_mine0, width=30)
    entry_mine1.delete(0, "end")
    entry_mine1.insert(0, "请输入用户名")
    entry_mine2 = tkinter.Entry(root_mine0, width=30)
    entry_mine2.delete(0, "end")
    entry_mine2.insert(0, "请输入密码")
    button_mine = tkinter.Button(root_mine0, text="确定", command=lambda: Account_Sign_in(root_mine_tips))
    button_register = tkinter.Button(root_mine0, text="立即注册", command=lambda: register())

    root_mine_tips.pack()
    entry_mine1.pack()
    entry_mine2.pack()
    button_mine.pack()
    button_register.pack()

    root_mine0.protocol("WM_DELETE_WINDOW", mine_on_closing)
    root_mine0.mainloop()


mine_0()

# ---分割线---

root = tkinter.Tk()  # 创建主窗口
root.title("亿词斩")  # 主窗口命名
root.geometry("865x632")  # 设置窗口大小
root.resizable(False, False)  # 禁止窗口拉伸
root.protocol("WM_DELETE_WINDOW", mine_on_closing)
# 图片添加
canvas = tkinter.Canvas(root, height=865, width=932)
image_file = tkinter.PhotoImage(file="root_gif1.gif")
canvas.create_image(432.5, 316, image=image_file)
canvas.pack()

# ---分割线---

# 功能
def learn():
    global words_meaning, words, indexes, learn_entry, learn_tip1, learn_tip2, learn_button, root_learn, frequency
    root_learn = tkinter.Tk()
    root_learn.title("learn")
    root_learn.geometry("500x300")

    indexes = 0  # 正确答案索引
    words = get_words()  # 获取单词
    words_meaning = get_words_meaning()  # 获取单词意思
    frequency = [0 for i in range(0, len(words))]
    if frequency == []:
        learn_button1 = tkinter.Button(root_learn, text="单词已背完，去添加新的单词吧", command=root_learn.destroy)
        learn_button1.pack()
    else:
        learn_tip1 = tkinter.Label(root_learn, text="提示:%s" % (words_meaning[indexes]))
        learn_tip2 = tkinter.Label(root_learn, text="还剩余%d个单词,请回答" % (len(words)))
        learn_entry = tkinter.Entry(root_learn, width=30)  # 创建输入框
        learn_entry.delete(0, "end")
        learn_entry.insert(0, "请输入单词")  # 默认文本
        learn_button = tkinter.Button(root_learn, text="确定", command=game)

        learn_tip1.pack()
        learn_tip2.pack()
        learn_entry.pack()
        learn_button.pack()
    root_learn.mainloop()


def game():
    global words, words_meaning, indexes, game_round, frequency
    game_round = 0
    answer = learn_entry.get()
    answer = answer.lower()
    if answer == words[indexes]:
        game_round += 1
        if frequency[indexes] == 0:
            print("删除单词")
            db.db_manage.del_userword(user_id=user_id, words_name=words[indexes])
            del words[indexes]
            del words_meaning[indexes]
            del frequency[indexes]
            indexes -= 1
            game_round -= 1
        if game_round == len(words) - 1:
            frequency = [0 for i in range(0, len(words))]
        indexes += 1
        if indexes > len(words) - 1:
            indexes = 0
        if len(words) == 0:
            learn_tip2["text"] = "还剩余%d个单词,游戏结束" % (len(words))
            learn_tip1["text"] = "提示:无"
            learn_button["text"] = "退出"
            learn_button["command"] = root_learn.destroy
        else:
            learn_tip2["text"] = "还剩余%d个单词,回答正确" % (len(words))
            learn_tip1["text"] = "提示:%s" % (words_meaning[indexes])
            learn_entry.delete(0, "end")
            learn_entry.insert(0, '请输入单词')  # 默认文本
    else:
        frequency[indexes] = 1
        learn_tip2["text"] = "还剩余%d个单词,回答错误" % (len(words))


def get_words():
    if account_flag == 1:
        res = db.db_manage.find_userword(user_id)
        words = [i[0] for i in res]
        return words

def get_words_meaning():
    if account_flag == 1:
        res = db.db_manage.find_userword(user_id)
        words_meaning = [i[1] for i in res]
        return words_meaning

def mine_1():
    global root_mine1
    root_mine1 = tkinter.Tk()
    root_mine1.title("我的")
    root_mine1.geometry("300x500")

    mine_name = tkinter.Label(root_mine1, text="账户名:%s" % (account))
    words = get_words()
    mine_tips = tkinter.Label(root_mine1, text="目前还剩余%d个单词" % (len(words)))
    modify_button = tkinter.Button(root_mine1, text="修改账户或密码", command=modify1)
    mine1_button = tkinter.Button(root_mine1, text="帮助中心", command=help)

    mine_name.pack()
    mine_tips.pack()
    modify_button.pack()
    mine1_button.pack()
    root_mine1.mainloop()

def modify1():
    global modify_password, modify_account,root_modify,modify_tips
    root_mine1.destroy()
    root_modify = tkinter.Tk()
    root_modify.title("修改账户或密码")
    root_modify.geometry("500x300")

    modify_tips = tkinter.Label(root_modify, text=" ")
    modify_account = tkinter.Entry(root_modify, width=30)
    modify_account.delete(0, "end")
    modify_account.insert(0, "请输入新\旧账户名")
    modify_password = tkinter.Entry(root_modify, width=30)
    modify_password.delete(0, "end")
    modify_password.insert(0, "请输入新\旧密码")
    button_add = tkinter.Button(root_modify, text="确定", command=modify2)

    modify_tips.pack()
    modify_account.pack()
    modify_password.pack()
    button_add.pack()
    root_modify.mainloop()

def modify2():
    global account
    account_modify = modify_account.get()
    password = modify_password.get()
    if db.db_manage.account_modify(user_id, account_modify, password) == "用户账户密码修改成功":
        account = account_modify
        root_modify.destroy()
        mine_1()
    else:
        modify_tips["text"] = "修改失败"

def help():
    root_help = tkinter.Tk()
    root_help.title("help")
    root_help.geometry("500x300")
    gdt = tkinter.Scrollbar(root_help)
    gdt1 = tkinter.Scrollbar(root_help, orient="horizontal")
    gdt.pack(side="right", fill="y")
    gdt1.pack(side="bottom", fill="x")
    lb = tkinter.Listbox(root_help, selectborderwidth=5, width=500, yscrollcommand=gdt.set, xscrollcommand=gdt.set)
    for i in rule:
        lb.insert("end", str(i))
    lb.pack(side="left", fill="both")
    gdt.config(command=lb.yview)
    gdt1.config(command=lb.xview)
    root_help.mainloop()

def fun_words():
    global root_words
    root_words = tkinter.Tk()
    root_words.title("words")
    root_words.geometry("200x300")

    button_add = tkinter.Button(root_words, text="添加单词", command=words_add)
    words = get_words()
    words_meaning = get_words_meaning()
    gdt = tkinter.Scrollbar(root_words)
    gdt.pack(side="right", fill="y")
    lb = tkinter.Listbox(root_words, selectborderwidth=5, width=500, yscrollcommand=gdt.set, xscrollcommand=gdt.set)
    for i in range(len(words)):
        lb.insert("end", str(words[i]) + "  :  " + str(words_meaning[i]))
    lb.pack(side="left", fill="both")
    gdt.config(command=lb.yview)
    button_add.pack()
    root_words.mainloop()


def words_add():
    global entry_words, entry_meaning, root_add
    root_add = tkinter.Tk()
    root_add.title("添加单词")
    root_add.geometry("300x500")

    entry_words = tkinter.Entry(root_add, width=30)
    entry_words.delete(0, "end")
    entry_words.insert(0, "请输入单词")
    entry_meaning = tkinter.Entry(root_add, width=30)
    entry_meaning.delete(0, "end")
    entry_meaning.insert(0, "请输入单词意思")
    button_add = tkinter.Button(root_add, text="确定", command=add_transfer)

    entry_words.pack()
    entry_meaning.pack()
    button_add.pack()
    root_add.mainloop()


def add_transfer():
    words = entry_words.get()
    words_meaning = entry_meaning.get()
    db.db_manage.add_words(words, words_meaning)
    res = db.db_manage.add_words(words, words_meaning)
    db.db_manage.add_userwords(user_id, res[0])
    # tkinter.messagebox.showinfo(root_add, "添加成功")
    entry_words.delete(0, "end")
    entry_meaning.delete(0, "end")


def add_note():
    no = entry_note.get()
    print(db.db_manage.add_usernote(user_id, no))
    create_radio()

def modify_note():
    global note_val
    note_id = note_val.get()

    if len(note_radios) < 1:
        tkinter.messagebox.showinfo("提示", "您还没有笔记！")
    notee = ""
    for i in note_radios:
        if i['value'] == note_id:
            notee = entry_note.get()
    print(db.db_manage.update_usernote(user_id=user_id, note_id=note_id, note=notee))

    create_radio()

def del_note():
    global note_val
    print("note_val.get():", note_val.get())
    if len(note_radios) < 1:
        tkinter.messagebox.showinfo("提示", "您还没有笔记！")
    print(db.db_manage.del_usernote(user_id=user_id, note_id=note_val.get()))
    create_radio()


def create_radio():
    global note_val
    for i in note_radios:
        i.grid_forget()
    note_radios.clear()
    res = db.db_manage.find_note(user_id)
    note_val = IntVar(root_note)
    if len(res) > 1:
        note_val.set(res[0][0])

    for idd, num, note in res:
        # print(idd)
        b = tkinter.Radiobutton(root_note, text=note, variable=note_val, value=idd, justify='left',command=print_val)
        note_radios.append(b)
    entry_note.delete(0, 'end')
    # note_val.set(4)

    i = 2
    for b in note_radios[::-1]:
        b.grid(row=i, column=0, columnspan=3, sticky='w')
        # print(b["value"])
        i += 1

def print_val():
    print(note_val.get())

def note_on_closing():
    root_note.destroy()
    note_radios.clear()

def note():
    global root_note, entry_note, note_val, note_radios, modify_button
    root_note = tkinter.Tk()
    root_note.title("note")
    root_note.geometry("200x300")

    add_button = tkinter.Button(root_note, text="添加", width=8, command=add_note)
    modify_button = tkinter.Button(root_note, text="修改", width=8, command=modify_note)
    del_button = tkinter.Button(root_note, text="删除", width=8, command=del_note)
    entry_note = tkinter.Entry(root_note, width=25)
    add_button.grid(row=0, column=0)
    modify_button.grid(row=0, column=1)
    del_button.grid(row=0, column=2)
    entry_note.grid(row=1, column=0, columnspan=3)
    create_radio()
    root_note.protocol("WM_DELETE_WINDOW", note_on_closing)
    root_note.mainloop()


# ---分割线---

canvas.bind('<Button-1>', lambda event: bind_1(event))  # 关联鼠标点击事件
canvas.bind('<Motion>', lambda event: where(event))  # 关联鼠标经过事件


# 按钮
def bind_1(event):  # 点击响应函数
    if 80 <= event.x <= 280 and 350 <= event.y <= 400:  # 响应的位置
        root.quit()
    elif 80 <= event.x <= 280 and 50 <= event.y <= 100:
        mine_1()
    elif 80 <= event.x <= 280 and 110 <= event.y <= 160:
        learn()
    elif 80 <= event.x <= 280 and 170 <= event.y <= 220:
        words_add()
    elif 80 <= event.x <= 280 and 230 <= event.y <= 280:
        fun_words()
    elif 80 <= event.x <= 280 and 290 <= event.y <= 340:
        note()


def where(event):
    if 80 <= event.x <= 280 and 350 <= event.y <= 400:
        bind_2(rootbutton_quite_r1, rootbutton_quite_r2, rootbutton_quite_t)
    elif 80 <= event.x <= 280 and 50 <= event.y <= 100:
        bind_2(rootbutton1_r1, rootbutton1_r2, rootbutton1_t)
    elif 80 <= event.x <= 280 and 110 <= event.y <= 160:
        bind_2(rootbutton2_r1, rootbutton2_r2, rootbutton2_t)
    elif 80 <= event.x <= 280 and 170 <= event.y <= 220:
        bind_2(rootbutton3_r1, rootbutton3_r2, rootbutton3_t)
    elif 80 <= event.x <= 280 and 230 <= event.y <= 280:
        bind_2(rootbutton4_r1, rootbutton4_r2, rootbutton4_t)
    elif 80 <= event.x <= 280 and 290 <= event.y <= 340:
        bind_2(rootbutton5_r1, rootbutton5_r2, rootbutton5_t)
    else:
        canvas.itemconfigure(rootbutton1_r1, outline='white')  # 恢复外框默认颜色
        canvas.itemconfigure(rootbutton1_r2, outline='white')  # 恢复内框默认颜色
        canvas.itemconfigure(rootbutton1_t, fill='white')  # 恢复显示文本默认颜色
        canvas.itemconfigure(rootbutton2_r1, outline='white')  # 恢复外框默认颜色
        canvas.itemconfigure(rootbutton2_r2, outline='white')  # 恢复内框默认颜色
        canvas.itemconfigure(rootbutton2_t, fill='white')  # 恢复显示文本默认颜色
        canvas.itemconfigure(rootbutton3_r1, outline='white')  # 恢复外框默认颜色
        canvas.itemconfigure(rootbutton3_r2, outline='white')  # 恢复内框默认颜色
        canvas.itemconfigure(rootbutton3_t, fill='white')  # 恢复显示文本默认颜色
        canvas.itemconfigure(rootbutton4_r1, outline='white')  # 恢复外框默认颜色
        canvas.itemconfigure(rootbutton4_r2, outline='white')  # 恢复内框默认颜色
        canvas.itemconfigure(rootbutton4_t, fill='white')  # 恢复显示文本默认颜色
        canvas.itemconfigure(rootbutton5_r1, outline='white')  # 恢复外框默认颜色
        canvas.itemconfigure(rootbutton5_r2, outline='white')  # 恢复内框默认颜色
        canvas.itemconfigure(rootbutton5_t, fill='white')  # 恢复显示文本默认颜色
        canvas.itemconfigure(rootbutton_quite_r1, outline='white')  # 恢复外框默认颜色
        canvas.itemconfigure(rootbutton_quite_r2, outline='white')  # 恢复内框默认颜色
        canvas.itemconfigure(rootbutton_quite_t, fill='white')  # 恢复显示文本默认颜色


def bind_2(button1, button2, button3):  # 鼠标经过响应函数
    canvas.itemconfigure(button1, outline='black')  # 重设外框颜色
    canvas.itemconfigure(button2, outline='black')  # 重设内框颜色
    canvas.itemconfigure(button3, fill='black')  # 重设显示文本颜色


rootbutton_quite_r1 = canvas.create_rectangle(80, 350, 280, 400, width=1, outline='white')  # 按钮外框
rootbutton_quite_r2 = canvas.create_rectangle(80 + 3, 350 + 3, 280 - 3, 400 - 3, width=1, outline='white')  # 按钮内框
rootbutton_quite_t = canvas.create_text(180, 375, text='退 出', font=('宋体', 20, 'bold'), fill='white')  # 按钮显示文本

rootbutton1_r1 = canvas.create_rectangle(80, 50, 280, 100, width=1, outline='white')  # 按钮外框
rootbutton1_r2 = canvas.create_rectangle(80 + 3, 50 + 3, 280 - 3, 100 - 3, width=1, outline='white')  # 按钮内框
rootbutton1_t = canvas.create_text(180, 75, text="我 的", font=('宋体', 20, 'bold'), fill='white')  # 按钮显示文本

rootbutton2_r1 = canvas.create_rectangle(80, 110, 280, 160, width=1, outline='white')  # 按钮外框
rootbutton2_r2 = canvas.create_rectangle(80 + 3, 110 + 3, 280 - 3, 160 - 3, width=1, outline='white')  # 按钮内框
rootbutton2_t = canvas.create_text(180, 135, text="学 习", font=('宋体', 20, 'bold'), fill='white')  # 按钮显示文本

rootbutton3_r1 = canvas.create_rectangle(80, 170, 280, 220, width=1, outline='white')  # 按钮外框
rootbutton3_r2 = canvas.create_rectangle(80 + 3, 170 + 3, 280 - 3, 220 - 3, width=1, outline='white')  # 按钮内框
rootbutton3_t = canvas.create_text(180, 195, text="添加单词", font=('宋体', 20, 'bold'), fill='white')  # 按钮显示文本

rootbutton4_r1 = canvas.create_rectangle(80, 230, 280, 280, width=1, outline='white')  # 按钮外框
rootbutton4_r2 = canvas.create_rectangle(80 + 3, 230 + 3, 280 - 3, 280 - 3, width=1, outline='white')  # 按钮内框
rootbutton4_t = canvas.create_text(180, 255, text="单词表", font=('宋体', 20, 'bold'), fill='white')  # 按钮显示文本

rootbutton5_r1 = canvas.create_rectangle(80, 290, 280, 340, width=1, outline='white')  # 按钮外框
rootbutton5_r2 = canvas.create_rectangle(80 + 3, 290 + 3, 280 - 3, 340 - 3, width=1, outline='white')  # 按钮内框
rootbutton5_t = canvas.create_text(180, 315, text="笔 记", font=('宋体', 20, 'bold'), fill='white')  # 按钮显示文本

# ---分割线---

root.mainloop()
